package supershop3.controller;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import supershop3.dto.ProductDTO;
import supershop3.model.Product;
import supershop3.service.IProductService;

@Controller
@RequestMapping("/admin/start/product")
public class ProductController {

	@Resource
	private IProductService productService;
	
	//查询商品信息
	@RequestMapping("/findAll.do")
	@ResponseBody
	public Map findAll(ProductDTO dto) {
		List<Product> list = productService.findAll(dto);	
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("code",0);
		map.put("count",productService.getCount(dto));
		map.put("msg","查询成功");
		map.put("data",list);
		return map;
	}
	
	
	//上架/下架
	@RequestMapping("/putOn.do")
	@ResponseBody
	public int putOn(int id,String isPutOnSale) {
		return productService.putaway(id, isPutOnSale);
	}
	
	//推荐/取消推荐
	@RequestMapping("/recommend.do")
	@ResponseBody
	public int recommend(int id,String isToday) {
		return productService.recommend(id, isToday);
	}
	
	//添加商品
	@RequestMapping("/addOrEdit.do")
	@ResponseBody
	public int addOrEdit(Product p) {
		return productService.addOrEdit(p);
	}
	
	//删除商品-逻辑删除
	@RequestMapping("/del.do")
	@ResponseBody
	public int del(int id) {
		return productService.del(id);
	}
	
	//上传图片
	@RequestMapping("/upload.do")
	@ResponseBody
	public Map upload(MultipartFile file) {
		//获取文件名
		String name = file.getOriginalFilename();
		System.out.println(name);
		String path = "d:\\upload3\\"+name;
		try {
			file.transferTo(new File(path));
		} catch (IllegalStateException | IOException e) {
			e.printStackTrace();
		}
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("code", 0);
		map.put("msg", "");
		map.put("data", name);
		return map;
	}
	
	//修改数据库url
	@RequestMapping("/updateUrl.do")
	@ResponseBody
	public int updateUrl(int id,String url) {
		return productService.updateUrl(id,url);
	}
}
